package com.zcareu.logServ.controller;

import com.zcareu.logServ.service.AgentServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AgentController {

    @Autowired
    AgentServ agentServ;

    @GetMapping(value = "/agent")
    public Map<String, List<Map<String, Object>>> index(){
        Map<String, List<Map<String, Object>>> result = new LinkedHashMap<>();

        for(String group : agentServ.getGroups()) {
            result.put(group, agentServ.getAgents(group));
        }

        return result;
    }

    @PostMapping(value = "/agent")
    public Map save(@RequestParam String uuid, @RequestParam String hostname, @RequestParam String machineGroup, @RequestParam String displayName, @RequestParam String identifier) {
        Map result = new LinkedHashMap();
        result.put("success", agentServ.addAgent(uuid, hostname, machineGroup, displayName, identifier));
        return result;
    }
}
